<template lang="pug">
mt-dialog(
  v-model="appmodel"
  cancel-text="取消"
  title="上报危急值"
  :on-close="updateModel"
  :on-cancel="updateModel"
  :on-confirm="updateConfirmModel"
  size="small"
)
  el-from
    el-row(style="background-color: #f7fafc;width: 550px;height: 50px")
      el-col(:span="5")
        span.el-descriptions__label 姓名：
        span.el-descriptions__content(style="font-weight: 600;color: black") {{ props.selectionRow.patientName }}
      el-col(:span="4")
        span.el-descriptions__label 年龄：
        span.el-descriptions__content(style="font-weight: 600;color: black") {{ props.selectionRow.age }}
      el-col(:span="6") 
        span.el-descriptions__label 科室：
        span.el-descriptions__content(style="font-weight: 600;color: black") {{data.listOffOptions?.find(i=>i.value==props.selectionRow.billingDepartment)?.label??props.selectionRow.billingDepartment}} 
      el-col(:span="8") 
        span.el-descriptions__label 仪器：
        span.el-descriptions__content(style="font-weight: 600;color: black") {{data.listequipmentAsOptions?.find(i=>i.value==props.selectionRow.deviceId)?.label}}   
      el-col(:span="10") 
        span.el-descriptions__label 检验日期：
        span.el-descriptions__content(style="font-weight: 600;color: black") {{ props.selectionRow.inspectionTime ? $utils.formatDate(props.selectionRow.inspectionTime, "YYYY-MM-DD"):"" }} 
      el-col(:span="12") 
        span.el-descriptions__label 标本号:
        span.el-descriptions__content(style="font-weight: 600;color: black") {{ props.selectionRow.specimenNumber }}
    SectionTitle(style="font-size: 5px;") 发生危急值通知内容(生化申请项目)
    mt-table(
      v-bind="tableProps"
      style="margin-top: 10px;height: 200px"
    )
    mt-form(
        v-bind="editFormProps"
        style="margin-top: 10px;"
      )
</template>

<script setup lang="ts">
import { useApi } from "@scmt/core-api";
import {
  type FormItemProps,
  type TableProps,
  useNotify,
  type EditFormProps,
} from "@scmt/common-components";
import { type SpecimenEntity } from "@scmt/api-lis-specimen";
import { useRequest } from "@scmt/common-hooks";
import { useUtils } from "@scmt/lib-utils";
import { SectionTitle } from "@scmt/common-components";

//添加 Props属性
const props = defineProps({
  selectionRow: {
    type: Object as PropType<SpecimenEntity>,
    default: () => {},
  },
});
const $api = useApi();
const $utils = useUtils();
const $notify = useNotify();
const editingObj = ref<{} | Object>({});
const criticalData = ref([]);

const appmodel = defineModel({
  default: false,
});
const emit = defineEmits(["message-to-parent"]);
const updateModel = () => {
  appmodel.value = false;
};

const { request, data, loadingMap } = useRequest({
  save: (q) => {
    return $api.lis.specimen.batchGenerateBarcode(q).then(() => {
      emit("message-to-parent");
      appmodel.value = false;
    });
  },
  batchCriticalValue: (q) => {
    return $api.lis.criticalValue.batchCriticalValue(q).then(() => {
      emit("message-to-parent");
      $notify.success("发送成功");

      appmodel.value = false;
    });
  },
  getPorject: (id) => {
    $api.lis.specimen.porject.get(id).then((res) => {
      if (res && res.data) {
        criticalData.value = res.data;
      }
    });
  },
  listOffOptions: () => {
    return $api.upms.org.listAsOptions();
  },
  listequipmentAsOptions: () => $api.lis.equipment.listAsOptions(),
});

const updateConfirmModel = () => {
  if (!editingObj?.value?.reportId) {
    $notify.error("发送人员不能为空");
    return;
  }
  request("batchCriticalValue", {
    baseProjectList: criticalData.value,
    specimenId: props.selectionRow.id,
    patientId: props.selectionRow.patientId,
    reportId: editingObj?.value?.reportId,
    remark: editingObj?.value?.remark,
    outpatientNumber: props.selectionRow.outpatientNumber,
    sourceId: props.selectionRow.sourceId,
    source: "lis",
  });
};

const editFormProps = computed<EditFormProps>(() => {
  const formItems: FormItemProps[] = [
    {
      prop: "remark",
      label: "备注内容:",
      required: false,
    },
    {
      prop: "reportId",
      label: "发送人员:",
      required: true,
      as: "select",
      options: {
        loadMethod: () => $api.user.listDoctorAsOptions({ officeId: "" }),
      },
    },
  ];

  const editProps: EditFormProps = {
    items: formItems,
    model: editingObj.value,
    columns: 1,
  };
  return editProps;
});

const tableProps = computed(() => {
  const tableProps: TableProps = {
    data: criticalData.value,
    columns: [
      {
        prop: "baseProjectName",
        label: "检验项目",
      },
      {
        prop: "result",
        label: "结果",
      },
      {
        prop: "unit",
        label: "单位",
      },
      {
        prop: "reference",
        label: "参考值",
      },
    ],
  };
  return tableProps;
});
watch(
  appmodel,
  (visible: boolean) => {
    if (visible) {
      request("listequipmentAsOptions");
      request("listOffOptions");
      editingObj.value = {};
      request("getPorject", props.selectionRow.id);
    }
  },
  {
    immediate: true,
  }
);
</script>
<style lang="less" scoped>
:deep(.section-title h4[data-v-e5468bb0]) {
  font-size: 15px;
  color: black;
}
</style>
